
from code.data.Answer import Answer
from code.data.User import User
from code.dao.user_management import search_user

from code.dao.answer_management import search_answer_by_homeworkID,update_answer,search_answer


class AnswerBackend:
    @staticmethod
    def commit_answer(userin:User,answerin:Answer):
        ret = 0
        answer_commit = Answer()
        if userin.userID is None:
            ret = 1001
        elif answerin.answerID is None:
            ret = 1012
        else:
            user = search_user(userin,"userID")[0]
            answer = search_answer(answerin, "answerID")[0]
            if user.userID != answer.userID:
                ret = 1013
            elif answerin.answer_content is None:
                ret = 1014
            elif len(answerin.answer_content) == 0 or answerin.answer_content.isspace():
                ret = 1025
            elif answer.status != 0:
                ret = 1015
            elif answerin.answer_file_path is not None:
                if answerin.answer_file_size > 1000000000:
                    ret = 1016
                else:
                    answer_commit.answer_file_path = answerin.answer_file_path
                    answer_commit.answer_file_size = answerin.answer_file_size
                    answer_commit.answerID = answer.answerID
                    answer_commit.answer_content = answerin.answer_content
                    answer_commit.status = 1
                    if update_answer(answer_commit, "answer") is True:
                        ret = 1
                    else:
                        ret = 0
        return ret

    @staticmethod
    def init_display_one_homework_all_answer_for_teacher(userin:User, answerin:Answer):
        ret = 0
        if userin.userID is None:
            ret = 1001
        elif answerin.homeworkID is None:
            ret = 1012
        else:
            user = search_user(userin, "userID")[0]
            answer = search_answer_by_homeworkID(answerin, "all")
            if user.type != "Teacher":
                ret = 1018
            elif answer is None:
                ret = 1019
            else:
                ret = answer.sort(key=lambda answer:answer.status)
        return ret

    @staticmethod
    def correcting_answer(userin:User,answerin:Answer()):
        ret = 0
        answer_commit = Answer()
        if userin.userID is None:
            ret = 1001
        elif answerin.answerID is None:
            ret = 1012
        else:
            user = search_user(userin, "userID")[0]
            answer = search_answer(answerin, "answerID")[0]
            if user.type !="Teacher":
                ret = 1017
            elif answer.status ==0:
                ret = 1020
            elif answer.status ==2:
                ret = 1021
            elif answerin.review_content is None:
                ret = 1022
            elif len(answerin.review_content) == 0 or answerin.review_content.isspace():
                ret = 1023
            elif answerin.answer_file_path is not None:
                if answerin.answer_file_size > 1000000000:
                    ret = 1024
                else:
                    answer_commit.answer_file_path = answerin.answer_file_path
                    answer_commit.answer_file_size = answerin.answer_file_size
                    answer_commit.review_content = answerin.review_content
                    answer_commit.status = 2
                    if update_answer(answer_commit, "review") is True:
                        ret = 1
                    else:
                        ret = 0
        return ret

    @staticmethod
    def display_one_answer_for_teacher(userin:User,answerin:Answer):
        ret = 0
        if userin.userID is None:
            ret = 1001
        elif answerin.answerID is None:
            ret = 1012
        else:
            user = search_user(userin, "userID")[0]
            answer = search_answer(answerin, "answerID")[0]
            if user.type != "Teacher":
                ret = 1017
            elif answer is None:
                ret = 1025
            else:
                ret = answer
        return answer

